//
//  HXLSuitcaseView.m
//  On The Load
//
//  Created by 晓琳 on 15/11/5.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLSuitcaseView.h"
#import "AppTools.h"
#import "UIImage+Blur.h"
#import "TodayModel.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>

@interface HXLSuitcaseView ()

@property (strong, nonatomic) IBOutlet UIImageView *backImageView;

@property (nonatomic, strong) UIImageView *backImageView1;
@property (nonatomic, strong) BaseView *bottomView;
@property (nonatomic, strong) NSArray *buttonTitleArray;
@property (nonatomic, strong) NSArray *buttonImageArray;
@property (nonatomic, strong) TodayModel *today;
@property(nonatomic,strong)UILabel *temperature_curr;

@property(nonatomic,strong)UILabel *weather;
@property(nonatomic,strong)UILabel *temperature;
@property(nonatomic,strong)UILabel *wind;
@property(nonatomic,strong)UILabel *winp;
@property(nonatomic,strong)UILabel *humidity;
@property (nonatomic, strong) UIImageView *weatherIcon;

@property (nonatomic, strong) UILabel *cityLable;


@end


/*
 "temperature": "14℃/9℃",
 "temperature_curr": "11℃",
 "humidity": "87%",
 "weather": "多云",
 "weather_icon": "http://api.k780.com:88/upload/weather/d/1.gif",
 "weather_icon1": "",
 "wind": "北风",
 "winp": "2级",
 */

@implementation HXLSuitcaseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.frame.size.width, HEIGHT)];
    NSLog(@"ssssss = %f",self.frame.size.width);
    
    [self addSubview:self.backImageView1];
    


    //根据时间判断图片
    if([self currentTimeLessThanNightTime:5+8 toHour:28]){
        UIImage *image = [UIImage imageNamed:@"back1.png"];
        self.backImageView1.image = [image boxblurImageWithBlur:0.7];
        
        
        self.backImageView1.image = [UIImage imageNamed:@"back1.png"];
    } else {
        UIImage *image = [UIImage imageNamed:@"earth.jpg"];
        self.backImageView1.image = [image boxblurImageWithBlur:0.7];
        
        
        self.backImageView1.image = [UIImage imageNamed:@"earth.jpg"];
        
    }

    self.temperature_curr = [[UILabel alloc] initWithFrame:CGRectMake(30*SCALEW,140*SCALEH, 170*SCALEW, 150 * SCALEH)];
//        self.temperature_curr.backgroundColor = [UIColor redColor];
    self.temperature_curr.textColor = [UIColor whiteColor];
    self.temperature_curr.font = [UIFont fontWithName:@"CourierNewPSMT" size:70 * SCALEH];
    [self addSubview:self.temperature_curr];
    
    
//    self.weatherIcon = [[UIImageView alloc] initWithFrame:CGRectMake(200*SCALEW, 120*SCALEH, 30*SCALEW, 30*SCALEH)];
//    [self addSubview:self.weatherIcon];
    
    self.weather = [[UILabel alloc] initWithFrame:CGRectMake(220 * SCALEW, 150 *  SCALEH, 150 * SCALEW, 40 * SCALEH)];
//        self.weather.backgroundColor = [UIColor redColor];
    self.weather.textColor = [UIColor whiteColor];
    self.weather.font = [UIFont fontWithName:@"CourierNewPSMT" size:20 * SCALEH];
    [self addSubview:self.weather];
    
    
    self.temperature = [[UILabel alloc] initWithFrame:CGRectMake(220 * SCALEW, 190 * SCALEH, 150 * SCALEW, 40 * SCALEH)];
//        self.temperature.backgroundColor = [UIColor redColor];
    self.temperature.textColor = [UIColor whiteColor];
    self.temperature.font = [UIFont fontWithName:@"CourierNewPSMT" size:20 * SCALEW];
    [self addSubview:self.temperature];
    
    self.wind = [[UILabel alloc] initWithFrame:CGRectMake(220 * SCALEW, 230 * SCALEH, 70 * SCALEW, 40 * SCALEH)];
//        self.wind.backgroundColor = [UIColor redColor];
    self.wind.textColor = [UIColor whiteColor];
    self.wind.font = [UIFont fontWithName:@"CourierNewPSMT" size:20 * SCALEW];
    [self addSubview:self.wind];
    
    self.winp = [[UILabel alloc] initWithFrame:CGRectMake(300 * SCALEW, 230 * SCALEH, 75 * SCALEW, 40 * SCALEH)];
//        self.winp.backgroundColor = [UIColor redColor];
    self.winp.textColor = [UIColor whiteColor];
    self.winp.font = [UIFont fontWithName:@"CourierNewPSMT" size:20 * SCALEW];
    [self addSubview:self.winp];
    
    self.humidity = [[UILabel alloc] initWithFrame:CGRectMake(220 * SCALEW, 270 * SCALEH, 150 * SCALEW, 40 * SCALEH)];
//    self.humidity.text = @"湿度";
//        self.humidity.backgroundColor = [UIColor redColor];
    self.humidity.textColor = [UIColor whiteColor];
    self.humidity.font = [UIFont fontWithName:@"CourierNewPSMT" size:20 * SCALEW];
    [self addSubview:self.humidity];
    
    
    

    self.cityLable = [[UILabel alloc] initWithFrame:CGRectMake(170 * SCALEW, 400 * SCALEH, 150 * SCALEW, 40 * SCALEH)];
    self.cityLable.textColor = [UIColor whiteColor];
    self.cityLable.font = [UIFont fontWithName:@"CourierNewPSMT" size:20 * SCALEW];
    [self addSubview:self.cityLable];


    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(200*SCALEW, 100*SCALEH, 1, 230*SCALEH)];
    lineView.backgroundColor = [UIColor whiteColor];
    [self addSubview:lineView];
    
    self.buttonImageArray = @[@"",@"",@"",@"",@"",@""];
//    self.buttonTitleArray = @[@"行程规划",@"行程天气",@"汇率转换",@"目的地攻略",@"记账本",@"紧急救援"];
    self.buttonTitleArray = @[@"行程规划",@"行程天气",@"汇率转换"];

    self.bottomView = [[BaseView alloc] initWithFrame:CGRectMake(0,  500*SCALEH, WIDTH, HEIGHT- 500*SCALEH)];
    NSLog(@"%f",self.frame.size.height);
    NSLog(@"%f",self.backImageView1.frame.origin.y + self.backImageView1.frame.size.height);
//    self.bottomView.backgroundColor = [UIColor redColor];
    [self addSubview:self.bottomView];
    

    //创建button视图
    [self buttonForBottomView];
    [self addHeaderRefresh];
    [self getTodayData:@"dalian"];
}

- (void)buttonForBottomView {
    
    for (int i = 0; i < self.buttonTitleArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bottomView addSubview:button];
        button.frame = CGRectMake((i % 3)* 125*SCALEW, (i / 3) * 125*SCALEH, 125*SCALEW, 125*SCALEH);
//        button.backgroundColor = [UIColor yellowColor];
        
            [self.bottomView addSubview:button];
        button.tag = 1000 + i;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:[self.buttonTitleArray objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 3;
        button.layer.borderColor = [UIColor whiteColor].CGColor;
            
        }
    
}


- (void)buttonAction:(UIButton *)button{
    [self.myDelegate pushNextViewController:button.tag];
}



- (BOOL) currentTimeLessThanNightTime:(NSInteger)fromHour toHour:(NSInteger)tohour
{

    NSDate *datefrom = [self getCustomDateWithHour:fromHour];
    NSDate *dateto = [self getCustomDateWithHour:tohour];
    NSDate *  nowdate=[NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger seconds = [zone secondsFromGMTForDate:nowdate];
    //获取当地时间
    NSDate *localDate = [NSDate dateWithTimeIntervalSinceNow:seconds];

    if ([localDate compare:datefrom] == NSOrderedDescending && [localDate compare:dateto] == NSOrderedAscending) {
        NSLog(@"该时间在%@-%@",datefrom, dateto);

        return YES;
    
    } else {
    NSLog(@"sss");
    return NO;
    
    }
}



- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    
    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    return [resultCalendar dateFromComponents:resultComps];
}

- (void) getTodayData:(NSString *)localCity {
    
    [AppTools getAFN:[NSString stringWithFormat:@"http://api.k780.com:88/?app=weather.today&weaid=%@&&appkey=15444&sign=0ae11d1a1780cbd0d768f016c19a8a1c&format=json",localCity] andpara:nil andBlock:^(id result) {
        
        self.today = [[TodayModel alloc] init];
        NSDictionary *bigDic = result;
        NSDictionary *resultDic = [bigDic objectForKey:@"result"];
        NSLog(@"result = %@",resultDic);
        [self.today setValuesForKeysWithDictionary:resultDic];
        NSLog(@" === %@",self.today.citynm);
        
        [self getValueForWeatherData];
        
    } andfailBlock:^(id result) {
        
        
    }];
    
    
    
}


- (void) getValueForWeatherData {
    if ([self.today.weather containsString:@"雨"]) {
        if([self currentTimeLessThanNightTime:5+7 toHour:27]){

            self.backImageView1.image = [UIImage imageNamed:@"rain"];
        }else {
            self.backImageView1.image = [UIImage imageNamed:@"yrain"];
            
        }
    }else if ([self.today.weather containsString:@"晴"]){
        if([self currentTimeLessThanNightTime:5+7 toHour:27]){
            
            self.backImageView1.image = [UIImage imageNamed:@"sun"];
        }else {
            self.backImageView1.image = [UIImage imageNamed:@"ysun"];
        }
        
    }else if ([self.today.weather containsString:@"云"]){
        if([self currentTimeLessThanNightTime:5+7 toHour:27]){
            
            self.backImageView1.image = [UIImage imageNamed:@"yun"];
        }else {
            self.backImageView1.image = [UIImage imageNamed:@"ywind"];
        }
    }else if ([self.today.weather containsString:@"雪"]){
        if([self currentTimeLessThanNightTime:5+7 toHour:27]){
            
            self.backImageView1.image = [UIImage imageNamed:@"snow"];
            //        [self createNowWeatherBackView];
        }else {
            self.backImageView1.image = [UIImage imageNamed:@"ysnow"];
        }
    }
    else if ([self.today.weather containsString:@"阴"]){
        self.backImageView1.image = [UIImage imageNamed:@"yin"];
        if([self currentTimeLessThanNightTime:5+7 toHour:27]){
            
            self.backImageView1.image = [UIImage imageNamed:@"yin"];
            //        [self createNowWeatherBackView];
        }else {
            self.backImageView1.image = [UIImage imageNamed:@"ywind"];
        }
    }else if ([self.today.weather containsString:@"霾"]){
        self.backImageView1.image = [UIImage imageNamed:@"mai"];
//        [self createNowWeatherBackView];
    }
    else
    {
        if([self currentTimeLessThanNightTime:5+8 toHour:28]){
        
            self.backImageView1.image = [UIImage imageNamed:@"back1.png"];
        //        [self createNowWeatherBackView];
    }else {
        self.backImageView1.image = [UIImage imageNamed:@"earth"];
    }
//        [self createNowWeatherBackView];
    }
    

    self.temperature.text = self.today.temperature;
    self.temperature_curr.text = self.today.temperature_curr;
    self.weather.text = self.today.weather;
    self.wind.text = self.today.wind;
    self.cityLable.text = self.today.citynm;
    self.winp.text = self.today.winp;
    self.humidity.text = [NSString stringWithFormat:@"湿度: %@",self.today.humidity];
//    [self.weatherIcon sd_setImageWithURL:[NSURL URLWithString:self.today.weather_icon] placeholderImage:[UIImage imageNamed:@""]];
    self.cityLable.text = self.today.citynm;
    
    
    
}


- (void)addHeaderRefresh {



}

@end
